package io.lemur.common.util.str;

import java.util.Arrays;

/**
 * 字符串的格式化
 * 
 * @author JueYue 2014年1月16日--上午11:49:03
 */
public class StrFormat {

    private final static String ONE_ZERO          = "0";
    private final static String TWO_ZERO          = "00";
    private final static String THREE_ZERO        = "000";
    private final static String FOUR_ZERO         = "0000";
    private final static String FIVE_ZERO         = "00000";
    private final static String SEX_ZERO          = "000000";

    private final static String ONE_SPACE         = " ";
    private final static String TWO_SPACE         = "  ";
    private final static String THREE_SPACE       = "   ";
    private final static String FOUR_SPACE        = "    ";
    private final static String FIVE_SPACE        = "     ";
    private final static String SEX_SPACE         = "      ";

    /**
     * 点之前的
     * @param amt
     * @param length
     * @return
     */
    public static String formatAmt(String amt, int length) {
        String amtStr = null;
        amtStr = amt.indexOf(".") > 0 ? amt.substring(0, amt.lastIndexOf("."))
                                        + amt.substring(amt.lastIndexOf(".") + 1) : amt;
        int len = length - amtStr.length();
        switch (len) {
            case 0:
                break;
            case 1:
                amtStr = ONE_ZERO + amtStr;
                break;
            case 2:
                amtStr = TWO_ZERO + amtStr;
                break;
            case 3:
                amtStr = THREE_ZERO + amtStr;
                break;
            case 4:
                amtStr = FOUR_ZERO + amtStr;
                break;
            case 5:
                amtStr = FIVE_ZERO + amtStr;
                break;
            case 6:
                amtStr = SEX_ZERO + amtStr;
                break;
            default:
                for (int i = 0; i < len; i++) {
                    amtStr = "0" + amtStr;
                }
                break;
        }
        return amtStr;
    }

    

    public static String getByteString(String data) {
        byte[] bytes = data.getBytes();
        StringBuilder sb = new StringBuilder();
        for (int i = 0, le = bytes.length; i < le; i++) {
            sb.append(bytes[i]);
        }
        return sb.toString();
    }

    /**
     * 长度不足,左侧添加0
     * 
     * @date 2014年1月10日
     * @param str
     *            字符串
     * @param len
     *            需求的长度
     * @return
     */
    public static String getFormatStr(String str, int len) {
        String ret = str;
        len = len - str.length();
        switch (len) {
            case 0:
                break;
            case 1:
                ret = ONE_ZERO + ret;
                break;
            case 2:
                ret = TWO_ZERO + ret;
                break;
            case 3:
                ret = THREE_ZERO + ret;
                break;
            case 4:
                ret = FOUR_ZERO + ret;
                break;
            case 5:
                ret = FIVE_ZERO + ret;
                break;
            case 6:
                ret = SEX_ZERO + ret;
                break;
            default:
                for (int i = 0; i < len; i++) {
                    ret = "0" + ret;
                }
                break;
        }
        return ret;
    }

    /**
     * 长度补足空格--右侧
     * 
     * @date 2014年1月10日
     * @param str
     * @param len
     * @return
     */
    public static String rightSpace(String str, int len) {
        String ret = str;
        len = len - str.length();
        switch (len) {
            case 0:
                break;
            case 1:
                ret = ret + ONE_SPACE;
                break;
            case 2:
                ret = ret + TWO_SPACE;
                break;
            case 3:
                ret = ret + THREE_SPACE;
                break;
            case 4:
                ret = ret + FOUR_SPACE;
                break;
            case 5:
                ret = ret + FIVE_SPACE;
                break;
            case 6:
                ret = ret + SEX_SPACE;
                break;
            default:
                for (int i = 0; i < len; i++) {
                    ret = ret + " ";
                }
                break;
        }
        return ret;
    }

    public static byte[] byteAdd(byte[] a, byte[] b) {
        byte[] ret = new byte[a.length + b.length];
        for (int i = 0, le = a.length; i < le; i++) {
            ret[i] = a[i];
        }
        for (int j = 0, le = b.length; j < le; j++) {
            ret[a.length + j] = b[j];
        }
        return ret;

    }

    public static byte[] byteSpace(byte[] a, int len) {
        byte[] ret = new byte[len];
        for (int i = 0, le = a.length; i < le; i++) {
            ret[i] = a[i];
        }
        for (int j = 0, le = len - a.length; j < le; j++) {
            ret[a.length + j] = 32;
        }
        return ret;
    }

    public static byte[] subByte(byte[] a, int start, int end) {
        return Arrays.copyOfRange(a, start, end);
    }

}
